#!python3
# -*- coding: utf-8

import socket
import sys
import logging

DEFAULT_PORT = 27015

def main():
    addrinfos = socket.getaddrinfo(
        host=None,
        port=DEFAULT_PORT,
        family=socket.AF_INET,
        type=socket.SOCK_DGRAM,
        proto=socket.IPPROTO_UDP,
        flags=socket.AI_PASSIVE
    )
    assert len(addrinfos) > 0
    family, socktype, protocol, canonname, addrinfo = addrinfos[0]
    try:
        with socket.socket(family=family, type=socktype, proto=protocol) as sock:
            sock.bind(addrinfo)

            bs, ret_addr = sock.recvfrom(256)
            ip, port = ret_addr
            print(f'Bytes received: {len(bs)}, from {ip}:{port}')
            n = sock.sendto(bs, ret_addr)
            print(f'Bytes Sent: {n}')
    except Exception as e:
        logging.exception(e)
        return 1

if __name__=='__main__':
    sys.exit(main())